<template>
	<view class="customerForm">
		<uni-forms ref="baseForm" :modelValue="formData" label-width="90px" :rules="rules">
			<uni-forms-item label="姓名" required name="UserName">
				<uni-easyinput v-model="formData.UserName" placeholder="请输入姓名" />
			</uni-forms-item>
			<uni-forms-item label="手机号" required name="UserPhone">
				<uni-easyinput type="number" v-model="formData.UserPhone" placeholder="请输入手机号" />
			</uni-forms-item>
		</uni-forms>
		<button type="primary" @click="submit">保 存</button>
	</view>
</template>

<script>
	import {
		regPhone
	} from '@/utils/regExp.js'
	export default {
		data() {
			return {
				formData: {},
				rules: {
					UserName: {
						rules: [{
							required: true,
							errorMessage: '请输入姓名',
						}]
					},
					UserPhone: {
						rules: [{
							required: true,
							errorMessage: '请输入手机号',
						}, {
							validateFunction: (rule, value, data, callback) => {
								if (!regPhone.test(value)) return callback('手机号格式错误')
								return true
							}
						}]
					}
				}
			}
		},
		onLoad(query) {
			if (query.Id) {
				this.getTheData(query.Id)
				uni.setNavigationBarTitle({
					title: '编辑客户'
				})
			}
			this.formData.SupplieId = uni.getStorageSync('CompanyInfo').Id // 公司
			this.formData.IsSubAccount = true // 子账户
		},
		methods: {
			getTheData(id) {
				this.$http("post", "/BO/CustomerInfo/GetTheData", {
					id
				}).then((res) => {
					if (res.Success) {
						this.formData = res.Data;
					} else uni.showToast({
						icon: 'none',
						title: res.Msg
					})
				});
			},
			submit() {
				this.$refs['baseForm'].validate().then(res => {
					this.formData.RealName = this.formData.UserName
					this.formData.newPwd = this.formData.UserPhone.substring(5, 11)
					console.log(this.formData);
					uni.showLoading()
					this.$http('post', "/Base_Manage/Base_User/SaveData", this.formData)
						.then((res) => {
							uni.hideLoading()
							if (res.Success) {
								uni.showToast({
									icon: 'none',
									title: '保存成功'
								})
								setTimeout(() => {
									uni.navigateBack()
								}, 1000)
							} else uni.showToast({
								icon: 'none',
								title: res.Msg
							})
						});
				}).catch(err => {
					console.log(err);
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.customerForm {
		padding: 20rpx;
		box-sizing: border-box;
	}
</style>